package com.huilian.iotbox.admin.controller;

import com.huilian.iotbox.admin.service.NoticeService;
import com.huilian.iotbox.admin.service.UserService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.controller.BaseController;
import com.huilian.iotbox.data.po.Notice;
import com.huilian.iotbox.data.vo.UserInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/notice")
public class NoticeController extends BaseController {

    @Resource
    private NoticeService noticeService;

    @Resource
    private UserService userService;

    @GetMapping("/list")
    public ServerResponse list(HttpServletRequest request, Notice notice) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        return noticeService.findListByKeyword(userInfo, notice);
    }


    /**
     * 添加公告
     *
     * @param notice
     * @return
     */
    @PostMapping
    public ServerResponse add(HttpServletRequest request, @RequestBody Notice notice) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        notice.setUserId(userInfo.getId());
        return noticeService.add(notice);
    }

    /**
     * 修改公告
     *
     * @param notice
     * @return
     */
    @PutMapping
    public ServerResponse update(HttpServletRequest request, @RequestBody Notice notice) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        return noticeService.update(notice);
    }

    /**
     * 删除公告
     *
     * @return
     */
    @DeleteMapping("/{id}")
    public ServerResponse delete(HttpServletRequest request, @PathVariable Integer id) {
        UserInfo userInfo = userService.getLoginUserInfo(request);

        return noticeService.delete(id);
    }
}
